package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.VehicleAddparam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.UserVo;
import cn.tedu._03vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.servlet.http.HttpSession;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/vehicle/")
@ResponseBody
@Validated   //处理非封装参数异常 只能在类上添加
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;
    @PostMapping("add")
    @Operation(summary = "添加车辆")
    @ApiOperationSupport(order = 10)
    public JsonResult addVehicle(@RequestBody VehicleAddparam vehicleAddparam, HttpSession session){
        log.debug("vehicleAddParam:"+vehicleAddparam);
        UserVo userVo = (UserVo) session.getAttribute("user");
        if (userVo == null){
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        Vehicle vehicle =new Vehicle();
        BeanUtils.copyProperties(vehicleAddparam,vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        log.debug("userId:"+session.getId());
        vehicle.setUserId(userVo.getId());
        log.debug("vehicle:"+vehicle);
        vehicleMapper.insertVehicle(vehicle);
        return JsonResult.ok(vehicle);
    }

    @GetMapping("list")
    @Operation(summary = "车辆列表")
    @ApiOperationSupport(order = 20)
    @Parameters(value = {
            @Parameter(name = "brand",description = "车辆品牌"),
            @Parameter(name = "license", description = "车辆牌照"),
            @Parameter(name = "vehicleListQuery",hidden = true)
    }
    )
    public JsonResult listVehicle(VehicleListQuery vehicleListQuery, HttpSession session){
        log.debug("vehicleListQuery = " + vehicleListQuery);
        UserVo userVO = (UserVo) session.getAttribute("user");
        if (userVO == null){ //未登录
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        //查询该用户的车辆列表
        Long userId = userVO.getId();
        List<VehicleListVO> vehicleListVOS= vehicleMapper.selectVehicleList(vehicleListQuery, userId);

        return JsonResult.ok(vehicleListVOS);
    }

    @Operation(summary = "逻辑删除车辆")
    @PostMapping("del/{id}")
    @ApiOperationSupport(order = 30)

    public JsonResult delVehicle(@Range(min = 1L,max = 500L,message = "车辆编号必须在1-500之间")@PathVariable Long id , HttpSession session){
//        if (id<0){
//            throw new IllegalArgumentException("id不能小于0");
////            return new JsonResult(StatusCode.OPERATION_FAILED,"id不能小于0");
//        }
        log.debug("id:"+id);
        UserVo userVo =(UserVo)session.getAttribute("user");
        if (userVo ==null ){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        Long userId = userVo.getId();
        vehicleMapper.delVehicleById(id,userId);

        return JsonResult.ok();
    }


}
